<?php

namespace seraph_pds;

// Exit if accessed directly
if( !defined( 'ABSPATH' ) )
	exit;

// #######################################################################
// Ui

class Ui
{
	// #######################################################################
	// Common controls

	static function HtmlLink( $text, $href, $newWnd = false, $prms = null )
	{
		$res = '';

		if( !empty( $href ) )
		{
			$linkPreContent = @$prms[ 'linkPreContent' ];
			if( $linkPreContent )
				$res .= $linkPreContent;

			$res .= '<a href="' . $href . '"';
			if( $newWnd && strpos( $href, 'mailto:' ) !== 0 )
				$res .= ' target="_blank"';

			$linkAttrs = @$prms[ 'linkAttrs' ];
			if( $linkAttrs )
				$res .= ' ' . $linkAttrs;

			$res .= '>';
		}
		else if( @$prms[ 'noTextIfNoHref' ] )
			return( '' );

		$res .= $text;

		if( !empty( $href ) )
		{
			$res .= '</a>';

			$linkAfterContent = @$prms[ 'linkAfterContent' ];
			if( $linkAfterContent )
				$res .= $linkAfterContent;
		}

		return( $res );
	}

	static function CheckBox( $text, $id, $checked = false, $addNames = false )
	{
		return( self::_CheckRadBox( 'checkbox', $text, array( 'id' => $id ), null, $checked, $addNames ) );
	}

	static function RadioBox( $text, $idGroup, $value, $default = false )
	{
		return( self::_CheckRadBox( 'radio', $text, array( 'name' => $idGroup ), $value, $default ) );
	}

	static private function _CheckRadBox( $type, $text, $attrs, $value = null, $checked = false, $addNames = false )
	{
		$res = '';

		$switches = '';
		if( is_array( $text ) )
		{
			$txtItems = $text;
			$text = '';

			foreach( $txtItems as $txtItem )
			{
				if( !is_array( $txtItem ) )
				{
					$text .= $txtItem;
					continue;
				}

				$switchOptions = '';

				$switchDefVal = null;
				if( isset( $txtItem[ 2 ] ) )
					$switchDefVal = $txtItem[ 2 ];

				$txtItemId = $txtItem[ 0 ];

				$text .= '<a href="#" id="' . $txtItemId . '_view' . '" onclick="var ctl = document.getElementById( \'' . $txtItemId . '\' ); var iNew = ctl.selectedIndex + 1; if( iNew >= ctl.options.length ) iNew = 0; if( ctl.selectedIndex != iNew ) { ctl.selectedIndex = iNew; ctl.onchange(); } return false;">';

				foreach( $txtItem[ 1 ] as $txtItemVal )
				{
					if( isset( $txtItemVal[ 2 ] ) && $txtItemVal[ 2 ] )
						$switchDefVal = $txtItemVal[ 0 ];

					$switchOptions .= '<option value="' . $txtItemVal[ 0 ] . '"';

					if( $switchDefVal == $txtItemVal[ 0 ] )
					{
						$text .= $txtItemVal[ 1 ];
						$switchOptions .= ' selected';
					}

					$switchOptions .= '>' . $txtItemVal[ 1 ] . '</option>';
				}

				$text .= '</a>';

				$switches .= '<select id="' . $txtItemId . '"' . ( $addNames ? ( ' name="' . $txtItemId . '"' ) : '' ) .  ' style="display: none;"';
				$switches .= ' onchange="var text = this.selectedIndex >= 0 ? this.options[ this.selectedIndex ].text : \'\'; document.getElementById( \'' . $txtItemId . '_view' . '\' ).text = text">';
				$switches .= $switchOptions;
				$switches .= '</select>';
			}
		}

		$res .= '<label><input type="' . $type . '"';

		$id = null;
		foreach( $attrs as $attrKey => $attrVal )
		{
			$res .= ' ' . $attrKey . '="' . $attrVal . '"';
			if( $attrKey == 'id' )
				$id = $attrVal;
		}

		if( $addNames )
			$res .= ' name="' . $id . '"';

		if( $value )
			$res .= ' value="' . $value . '"';
		if( $checked )
			$res .= ' checked="checked"';
		$res .= '/>' . $text . $switches . '</label>';
		return( $res );
	}

	static function ComboBox( $id, $items, $value, $addNames = false )
	{
		$res = '';

		$res .= '<select id="' . $id . '"';
		if( $addNames )
			$res .= ' name="' . $id . '"';
		$res .= '>';

		foreach( $items as $itemVal => $itemText )
		{
			$res .= '<option value="' . $itemVal . '"';
			if( $itemVal == $value )
				$res .= ' selected';
			$res .= '>' . $itemText . '</option>';
		}

		$res .= '</select>';

		return( $res );
	}

	static function TextBox( $id, $value, $attrs = null, $addNames = false )
	{
		return( self::_InputBox( 'text', $id, $value, $attrs, $addNames ) );
	}

	static function NumberBox( $id, $value, $attrs = null, $addNames = false )
	{
		return( self::_InputBox( 'number', $id, $value, $attrs, $addNames ) );
	}

	private static function _GetTagAttrs( $attrs )
	{
		$res = '';

		if( is_array( $attrs ) )
		{
			foreach( $attrs as $attr => $attrVal )
			{
				$res .= ' ' . $attr . '="';

				if( is_array( $attrVal ) )
				{
					foreach( $attrVal as $attrValItem => $attrValItemVal )
					{
						$res .= $attrValItem . ':' . $attrValItemVal . ';';
					}
				}
				else
					$res .= $attrVal;

				$res .= '"';
			}
		}
		else if( is_string( $attrs ) )
			$res .= ' ' . $attrs;

		return( $res );
	}

	private static function _InputBox( $type, $id, $value, $attrs = null, $addNames = false )
	{
		$res = '';

		$res .= '<input type="' . $type . '"';

		if( $id )
		{
			$res .= ' id="' . $id . '"';
			if( $addNames )
				$res .= ' name="' . $id . '"';
		}

		if( $value )
			$res .= ' value="' . $value . '"';

		$res .= self::_GetTagAttrs( $attrs );

		$res .= ' />';
		return( $res );
	}

	// #######################################################################
	// Post boxes

	const PostBoxes_Mode_2Cols			= 0;

	static function PostBoxes_Begin( $title, $mode = self::PostBoxes_Mode_2Cols )
	{
		$res = '';

		$res .= '<div id="poststuff" class="wrap">';
		$res .= '<h1>' . $title . '</h1>';
		$res .= '<div id="post-body" class="metabox-holder ';

		switch( $mode )
		{
		case self::PostBoxes_Mode_2Cols:	$res .= 'columns-2'; break;
		}

		$res .= '">';

		return( $res );
	}

	static function PostBoxes_End()
	{
		$res = '';

		$res .= '</div></div>';

		return( $res );
	}

	static function PostBoxes_Body_Begin()
	{
		$res = '';

		$res .= '<div id="post-body-content" style="margin-bottom: 0;">';

		return( $res );
	}

	static function PostBoxes_Body_End()
	{
		$res = '';

		$res .= '</div>';

		return( $res );
	}

	static function PostBoxes_Container_Begin( $idx = 1 )
	{
		$res = '';

		$res .= '<div id="postbox-container-' . $idx . '" class="postbox-container">';
		$res .= '<div id="side-sortables" class="meta-box-sortables ui-sortable">';

		return( $res );
	}

	static function PostBoxes_Container_End()
	{
		$res = '';

		$res .= '</div></div>';

		return( $res );
	}

	static function PostBox_Begin( $text, $id = null, $classes = null, $expandable = true )
	{
		$res = '';

		$res .= '<div' . ( $id ? ( ' id="' . $id . '"' ) : '' ) . ' class="postbox' . ( $classes ? ( ' ' . $classes ) : '' ) . '">';

		if( $expandable )
			$res .= '<button type="button" class="handlediv" aria-expanded="true"><span class="screen-reader-text">Toggle panel: ' . $text . '</span><span class="toggle-indicator" aria-hidden="true"></span></button>';

		$res .= '<h2 class="hndle ui-sortable-handle" style="cursor: inherit;"><span>' . $text . '</span></h2>';
		$res .= '<div class="inside">';

		return( $res );
	}

	static function PostBox_End()
	{
		$res = '';

		$res .= '</div>';
		$res .= '</div>';

		return( $res );
	}

	static function PostBox_BottomGroupPanel_Begin()
	{
		return( '<div class="postbox seraph_pds_postboxes_bottom_group_panel">' );
	}

	static function PostBox_BottomGroupPanel_End()
	{
		return( '</div>' );
	}

	// #######################################################################
	// Settings blocks

	static function SettBlock_Begin()
	{
		$res = '';

		$res .= '<table class="form-table"><tbody>';

		return( $res );
	}

	static function SettBlock_End()
	{
		$res = '';

		$res .= '</tbody></table>';

		return( $res );
	}

	static function SettBlock_Item_Begin( $label )
	{
		$res = '';

		$res .= '<tr valign="top">';
		$res .= '<th scope="row">' . $label . '</th>';
		$res .= '<td>';
		$res .= '<fieldset>';

		return( $res );
	}

	static function SettBlock_Item_End()
	{
		$res = '';

		$res .= '</fieldset></td></tr>';

		return( $res );
	}

	static function SettBlock_ItemSubTbl_Begin( $attrs = null )
	{
		$res = '';

		$res .= '<table class="seraph_pds_settings_sub_table" border="0" cellpadding="0" cellspacing="0"';
		$res .= self::_GetTagAttrs( $attrs );
		$res .= '><tbody>';

		return( $res );
	}

	static function SettBlock_ItemSubTbl_End()
	{
		$res = '';

		$res .= '</tbody></table>';

		return( $res );
	}

	static function Sett_SaveBtn( $name )
	{
		$res = '';

		$res .= '<input type="submit" class="button button-primary" name="' . $name . '" value="' . __( 'Save Changes', 'seraph_pds' ) . '" />';

		return( $res );
	}

	// #######################################################################
	// Banner message

	const MsgInfo					= 0;
	const MsgSucc					= 1;
	const MsgWarn					= 2;
	const MsgErr					= 3;

	const MsgOptDismissible			= 0x00000001;
	const MsgOptFade				= 0x00000002;

	static function BannerMsg( $severity, $text, $opts = 0, $attrs = NULL )
	{
		if( empty( $text ) )
			return( '' );

		$class = NULL;
		switch( $severity )
		{
			case Ui::MsgSucc:		$class = 'notice notice-success'; break;
			case Ui::MsgWarn:		$class = 'notice notice-warning'; break;
			case Ui::MsgErr:		$class = 'notice notice-error'; break;

			default:				$class = 'notice notice-info'; break;
		}

		if( $opts & Ui::MsgOptDismissible )
			$class .= ' is-dismissible';
		if( $opts & Ui::MsgOptFade )
			$class .= ' fade';

		$res = '';
		$res .= '<div';

		if( $attrs )
			$res .= ' ' . $attrs;

		$res .= ' class = "' . $class . '"><p>' . $text . '</p>';

//		if( $opts & Ui::MsgOptDismissible )
//			$res .= '<button type="button" class="notice-dismiss"><span class="screen-reader-text">' . __( 'Dismiss this notice.', 'seraph_pds' ) . '</span></button>';

		$res .= '</div>';

		return( $res );
	}

	// #######################################################################
	// Help button

	const AdminHelpBtnModeBlockHeader		= 'blkhdr';
	const AdminHelpBtnModeChkRad			= 'chkrad';
	const AdminHelpBtnModeText				= 'txt';

	static function AdminHelpBtn( $href, $mode, $newWnd = true )
	{
		return( self::HtmlLink( '&nbsp;', $href, $newWnd, array( 'noTextIfNoHref' => true, 'linkPreContent' => '&nbsp;', 'linkAttrs' => 'class="seraph_pds_hlpbtn dashicons dashicons-editor-help ' . $mode . '"' ) ) );
	}
}

// #######################################################################
// #######################################################################
